package com.duoec.magichour.controller.api;

import com.duoec.magichour.core.annotation.Access;
import com.duoec.magichour.core.web.BaseApiController;
import com.duoec.magichour.dto.BaseResponse;
import com.duoec.magichour.dto.UserInfo;
import com.duoec.magichour.dto.request.ArticleSave;
import com.duoec.magichour.dto.response.ArticleDetailDto;
import com.duoec.magichour.dto.response.ArticleListItemDto;
import com.duoec.magichour.service.ArticleCateService;
import com.duoec.magichour.service.ArticleService;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章接口
 */
@RestController
@RequestMapping("/api/article")
public class ArticleApiController extends BaseApiController {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleCateService articleCateService;

    /**
     * 发布、编辑文章
     *
     * @param request 发布的文章信息
     * @return
     */
    @Access
    @RequestMapping(method = RequestMethod.POST)
    public BaseResponse<ArticleListItemDto> save(
            @RequestBody ArticleSave request
    ) {
        //参数校验

        //标题
        String title = request.getTitle();
        if (Strings.isNullOrEmpty(title) || title.length() < 3) {
            return BaseResponse.error(BaseResponse.CODE_PARAM_INVALID, "标题长度不能小于3个字符！");
        }

        //图片
        List<String> photos = request.getPhotos();
        if (photos == null || photos.isEmpty()) {
            return BaseResponse.error(BaseResponse.CODE_PARAM_INVALID, "图片不能为空！");
        }

        //内容
        String content = request.getContent();
        if (Strings.isNullOrEmpty(content) || content.length() < 10) {
            return BaseResponse.error(BaseResponse.CODE_PARAM_INVALID, "文章内容不能小于10个字符！");
        }

        //分类
        Integer cateId = request.getCateId();
        if (cateId != null && cateId > 0 && !articleCateService.exists(cateId)) {
            return BaseResponse.error(BaseResponse.CODE_PARAM_INVALID, "文章分类不存在或已被删除！");
        }

        ArticleListItemDto article = articleService.save(getUserInfo(), request);
        return BaseResponse.success(article);
    }

    /**
     * 获取推荐文章列表
     *
     * @param pageNo   当前分页数，1为第一页
     * @param pageSize 每页最大记录数
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/recommend")
    public BaseResponse<List<ArticleListItemDto>> recommendArticles(
            @RequestParam int pageNo,
            @RequestParam(required = false, defaultValue = "20") int pageSize
    ) {
        //获取当前用户的ID
        UserInfo userInfo = getUserInfo();
        long userId = userInfo == null ? 0 : userInfo.getId();
        List<ArticleListItemDto> articleListList = articleService.getRecommends(userId, pageNo, pageSize);
        return BaseResponse.success(articleListList);
    }

    /**
     * 获取某个用户{authorUserId}的文章列表
     *
     * @param authorUserId 用户ID
     * @param pageNo       当前分页数，1为第一页
     * @param pageSize     每页最大记录数
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/user/{authorUserId:\\d+}")
    public BaseResponse<List<ArticleListItemDto>> userArticles(
            @PathVariable long authorUserId,
            @RequestParam int pageNo,
            @RequestParam(required = false, defaultValue = "20") int pageSize
    ) {
        //获取当前用户的ID
        long authUserId = getUserInfo().getId();
        List<ArticleListItemDto> articleListList = articleService.getUserArticles(authUserId, authorUserId, pageNo, pageSize);
        return BaseResponse.success(articleListList);
    }

    /**
     * 获取已收藏文章列表
     *
     * @param pageNo   当前分页数，1为第一页
     * @param pageSize 每页最大记录数
     * @return
     */
    @Access
    @RequestMapping(method = RequestMethod.GET, value = "/collection")
    public BaseResponse<List<ArticleListItemDto>> collectionArticles(
            @RequestParam int pageNo,
            @RequestParam(required = false, defaultValue = "20") int pageSize
    ) {
        //获取当前用户的ID
        long userId = getUserInfo().getId();
        List<ArticleListItemDto> articleListList = articleService.getCollection(userId, pageNo, pageSize);
        return BaseResponse.success(articleListList);
    }

    /**
     * 查询文章列表
     *
     * @param keyword  搜索关键词
     * @param pageNo   当前分页数，1为第一页
     * @param pageSize 每页最大记录数
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/search")
    public BaseResponse<List<ArticleListItemDto>> searchArticles(
            @RequestParam String keyword,
            @RequestParam int pageNo,
            @RequestParam(required = false, defaultValue = "20") int pageSize
    ) {
        //获取当前用户的ID
        long userId = getUserInfo().getId();
        List<ArticleListItemDto> articleListList = articleService.searchArticles(userId, keyword, pageNo, pageSize);
        return BaseResponse.success(articleListList);
    }

    /**
     * 获取文章详细信息
     *
     * @param id 文章ID
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/{id:\\d+}")
    public BaseResponse<ArticleDetailDto> getDetail(
            @PathVariable long id
    ) {
        //获取当前用户的ID
        long userId = getUserInfo().getId();
        ArticleDetailDto article = articleService.getDetail(userId, id);
        if (article == null) {
            BaseResponse.notFindError("文章找不到或已被删除！");
        }
        return BaseResponse.success(article);
    }
}
